package com.zy.garbage.controller;

import com.zy.garbage.form.CreateGarbageForm;
import com.zy.garbage.form.FileForm;
import com.zy.garbage.form.FileOldForm;
import com.zy.garbage.service.FileService;
import com.zy.garbage.util.FileUtil;
import com.zy.garbage.util.ResultUtil;
import com.zy.garbage.vo.ImageVO;
import com.zy.garbage.vo.ResultVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;

@RestController
@RequestMapping("/file")
public class FileController extends BaseController{

    @Autowired
    FileService fileService;

    // 日志打印
    private Logger log = LoggerFactory.getLogger("FileController");

    @PostMapping("/uploadImage")
    public ResultVO uploadFile(
            @RequestParam("file") MultipartFile file,
            @RequestParam("name") String name,
            @RequestParam("typeId") String typeId,
            HttpServletRequest request)throws Exception{
        try {
            FileForm fileForm = new FileForm();
            fileForm.setFile(file);
            fileForm.setName("image");
            fileForm.setTypeId("image");
            ImageVO imageVO = fileService.saveImage(fileForm,request);
            return ResultUtil.success(imageVO);
        } catch (Exception e){
            log.error(e.toString());
        }
        return ResultUtil.error("上传错误");
    }
}
